package com.recruit.controller;

import com.recruit.dto.resume.CreateOrUpdateResumeDTO;
import com.recruit.model.EducationDO;
import com.recruit.model.ExperienceDO;
import com.recruit.model.ProjectDO;
import com.recruit.model.ResumeDO;
import com.recruit.service.EducationService;
import com.recruit.service.ExperienceService;
import com.recruit.service.ProjectService;
import com.recruit.service.ResumeService;
import com.recruit.vo.CreatedVO;
import com.recruit.vo.DeletedVO;
import com.recruit.vo.ResumeVO;
import com.recruit.vo.UpdatedVO;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.Logger;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Positive;
import java.util.List;

/**
 * @author denghufu
 */
@RestController
@RequestMapping("/recruit/resume")
@PermissionModule(value = "简历")
@Api(tags = "简历操作接口")
public class ResumeController {

    @Resource
    private ResumeService resumeService;

    @Resource
    private EducationService educationService;

    @Resource
    private ProjectService projectService;

    @Resource
    private ExperienceService experienceService;

    /**
     * 创建简历
     *
     * @param validator
     * @return
     */
    @PostMapping("")
    @GroupRequired
    @PermissionMeta(value = "创建简历")
    @ApiOperation(value = "创建简历")
    public CreatedVO create(
            @ApiParam(value = "创建简历信息实体", required = true)
            @RequestBody @Validated CreateOrUpdateResumeDTO validator) {
        resumeService.createResume(validator);
        return new CreatedVO(1000);
    }

    /**
     * 根据用户id更新简历
     *
     * @param userId
     * @return
     */
    @PutMapping("/{userId}")
    @GroupRequired
    @PermissionMeta(value = "更新简历")
    @ApiOperation(value = "根据用户id更新简历")
    public UpdatedVO update(
            @ApiParam(value = "用户id", required = true)
            @PathVariable @Positive(message = "{id.positive}") Integer userId,
            @ApiParam(value = "修改简历实体", required = true)
            @RequestBody @Validated CreateOrUpdateResumeDTO validator) {
        // 根据用户id查询简历
        ResumeDO resumeDO = resumeService.getSimpleByUserId(userId);
        if (resumeDO == null) {
            throw new NotFoundException(20010);
        }
        // 更新简历
        resumeService.updateResume(resumeDO, validator);
        return new UpdatedVO(1010);
    }

    /**
     * 根据用户id查询简历（每个用户只绑定一份简历）
     *
     * @param userId
     * @return
     */
    @Logger(template = "查看简历")
    @GetMapping("/get/{userId}")
    @GroupRequired
    @PermissionMeta(value = "查询简历")
    @ApiOperation(value = "根据用户id查询简历")
    public ResumeVO getByUserId(
            @ApiParam(value = "用户id", required = true)
            @PathVariable(value = "userId") @Positive(message = "{id.positive}") Integer userId) {
        ResumeVO resume = resumeService.getByUserId(userId);
        if (resume == null) {
            throw new NotFoundException(20010);
        }
        return resume;
    }

    /**
     * 根据id查询简历
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询简历")
    public ResumeVO get(
            @ApiParam(value = "简历id", required = true)
            @PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {

        ResumeDO resume = resumeService.getById(id);
        ResumeVO resumeVO = resumeService.getByUserId(resume.getUserId());
        if (resumeVO == null) {
            throw new NotFoundException(20020);
        }
        return resumeVO;
    }

    /**
     * 根据简历id拿到所有教育经历
     *
     * @param rid
     * @return
     */
    @GetMapping("/education/{rid}")
    @ApiOperation(value = "根据简历id拿到所有教育经历")
    public List<EducationDO> getEducations(
            @ApiParam(value = "简历id", required = true)
            @PathVariable(value = "rid") @Positive(message = "{rid.positive}") Integer rid) {
        return educationService.getByResumeId(rid);
    }

    /**
     * 更新教育经历
     *
     * @param
     * @return
     */
    @PutMapping("/education/update")
    @ApiOperation(value = "更新教育经历")
    public UpdatedVO updateEducation(
            @ApiParam(value = "教育实体信息", required = true)
            @RequestBody EducationDO educationDO) {
        // 更新
        educationService.updateById(educationDO);
        return new UpdatedVO(1010);
    }

    /**
     * 添加教育经历
     *
     * @param educationDO
     * @return
     */
    @PostMapping("/education")
    @ApiOperation(value = "添加教育经历")
    public CreatedVO addEducation(
            @ApiParam(value = "教育实体信息", required = true)
            @RequestBody EducationDO educationDO) {
        // 新增
        educationService.save(educationDO);
        return new CreatedVO(1000);
    }

    /**
     * 根据id删除教育经历
     *
     * @param id
     * @return
     */
    @DeleteMapping("/education/{id}")
    @ApiOperation(value = "根据id删除教育经历")
    public DeletedVO delEducation(
            @ApiParam(value = "id", required = true)
            @PathVariable Integer id) {
        educationService.removeById(id);
        return new DeletedVO(2000);
    }

    /**
     * 根据简历id拿到所有项目经历
     *
     * @param rid
     * @return
     */
    @GetMapping("/project/{rid}")
    @ApiOperation(value = "根据简历id拿到所有项目经历")
    public List<ProjectDO> getProjects(
            @ApiParam(value = "简历id", required = true)
            @PathVariable(value = "rid") @Positive(message = "{rid.positive}") Integer rid) {
        List<ProjectDO> projectDOS = projectService.getByResumeId(rid);
        return projectDOS;
    }

    /**
     * 更新项目经历
     *
     * @param
     * @return
     */
    @PutMapping("/project/update")
    @ApiOperation(value = "更新项目经历")
    public UpdatedVO updateProject(
            @ApiParam(value = "项目实体信息", required = true)
            @RequestBody ProjectDO projectDO) {
        // 更新
        projectService.updateById(projectDO);
        return new UpdatedVO(1010);
    }

    /**
     * @param projectDO
     * @return
     */
    @PostMapping("/project")
    @ApiOperation(value = "添加项目经历")
    public CreatedVO addProject(
            @ApiParam(value = "项目实体信息", required = true)
            @RequestBody ProjectDO projectDO) {
        // 新增
        projectService.save(projectDO);
        return new CreatedVO(1000);
    }

    /**
     * 根据id删除项目经历
     *
     * @param id
     * @return
     */
    @DeleteMapping("/project/{id}")
    @ApiOperation(value = "根据id删除项目经历")
    public DeletedVO delProject(
            @ApiParam(value = "id", required = true)
            @PathVariable Integer id) {
        projectService.removeById(id);
        return new DeletedVO(2000);
    }

    /**
     * 根据简历id拿到所有实习经历
     *
     * @param rid
     * @return
     */
    @GetMapping("/experience/{rid}")
    @ApiOperation(value = "根据简历id拿到所有实习经历")
    public List<ExperienceDO> getExperiences(
            @ApiParam(value = "简历id", required = true)
            @PathVariable(value = "rid") @Positive(message = "{rid.positive}") Integer rid) {
        return experienceService.getByResumeId(rid);
    }

    /**
     * 更新实习经历
     *
     * @param
     * @return
     */
    @PutMapping("/experience/update")
    @ApiOperation(value = "更新实习经历")
    public UpdatedVO updateExperience(
            @ApiParam(value = "实习经历实体", required = true)
            @RequestBody ExperienceDO experienceDO) {
        // 更新
        experienceService.updateById(experienceDO);
        return new UpdatedVO(1010);
    }

    /**
     * @param experienceDO
     * @return
     */
    @PostMapping("/experience")
    @ApiOperation(value = "新增实习经历")
    public CreatedVO addExperience(
            @ApiParam(value = "实习经历实体", required = true)
            @RequestBody ExperienceDO experienceDO) {
        // 新增
        experienceService.save(experienceDO);
        return new CreatedVO(1000);
    }

    /**
     * 根据id删除实习经历
     *
     * @param id
     * @return
     */
    @DeleteMapping("/experience/{id}")
    @ApiOperation(value = "根据id删除实习经历")
    public DeletedVO delExperience(
            @ApiParam(value = "id", required = true)
            @PathVariable Integer id) {
        //todo - 做类似的判断处理
        experienceService.removeById(id);
        return new DeletedVO(2000);
    }

}
